<?php

namespace App\Models\Depot;

// 引入模块
use Illuminate\Database\Eloquent\Model;
use App\Traits\SerializeDate;
// 退货表
class Back  extends Model
{
    use SerializeDate;
    // 设置当前模型对应的完整数据表名称
    protected $table = 'pre_depot_back';

    protected $dateFormat = 'U';
    // 定义创建时间的字段
    const CREATED_AT = 'createtime';

    const UPDATED_AT = null;

    // 允许批量赋值
    protected $guarded = [];

    protected $casts = [
        'createtime'   => 'datetime:Y-m-d H:i:s'
    ];

    protected $appends = [
        'address_text'  //地址
    ];

    // 格式化地址
    public function getAddressTextAttribute($value)
    {
        // 获取地区模型
        $Regions = new \App\Models\Region();

        $region = '';
        // 省
        $province = empty($this->attributes['province']) ? '' : $this->attributes['province'];
        if ($province) {
            $province_text = $Regions->where('code', $province)->value('name');
            $region = $province_text;
        }
        // 市
        $city = empty($this->attributes['city']) ? '' : $this->attributes['city'];
        if ($city) {
            $city_text = $Regions->where('code', $city)->value('name');
            $region .= $city_text;
        }
        // 区
        $district = empty($this->attributes['district']) ? '' : $this->attributes['district'];
        if ($district) {
            $district_text = $Regions->where('code', $district)->value('name');
            $region .= $district_text;
        }

        return $region . $this->attributes['address'];
    }
}
